import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import pickle
from tqdm import tqdm

# 步骤1：加载并拆分数据集
digits = load_digits()
X = digits.data
y = digits.target
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 步骤2：遍历K值并评估模型
k_values = range(1, 41)
accuracies = []
for k in tqdm(k_values, desc="Finding optimal K"):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)

# 步骤3：确定最优K值
max_accuracy = max(accuracies)
best_k = k_values[accuracies.index(max_accuracy)]
print(f"最优K值: {best_k}, 最优准确率: {max_accuracy:.4f}")

# 步骤4：绘制并保存准确率折线图
plt.figure(figsize=(10, 6))
plt.plot(k_values, accuracies, marker='o', linestyle='-', color='blue')
plt.axvline(x=best_k, color='red', linestyle='--')
plt.text(best_k + 0.5, max_accuracy, f'k={best_k}, Accuracy={max_accuracy:.4f}', color='red')
plt.xlabel('k value')
plt.ylabel('Accuracy')
plt.title('Accuracy of different k values')
plt.grid(True)
plt.savefig('accuracy_plot.pdf')
plt.close()

# 步骤5：保存最优KNN模型
best_knn = KNeighborsClassifier(n_neighbors=best_k)
best_knn.fit(X_train, y_train)
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn, f)